package com.toe.forum.webapi.service.impl;

import com.toe.common.restful.JsonPage;
import com.toe.forum.service.ForumArticleService;
import com.toe.forum.service.ISearchService;
import com.toe.forum.webapi.ESRepository.ArticleElasticsearchRepository;
import com.toe.pojo.entity.forum.ArticleElasticsearch;
import com.toe.pojo.vo.forum.ArticleDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
// 实现查询远程服务器ES的业务逻辑层实现类
@Service
@Slf4j
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private ForumArticleService forumArticleService;

    // 注入SpringData操作Es的持久层对象
    @Autowired
    private ArticleElasticsearchRepository repository;

    @Override
    public void loadArticle() {
        log.info("向ES中存入数据!!!!!!!!!!!!!!!!");
        int i = 1; //循环次数,以为就是页码
        List<ArticleDetailVO> articleDetailVOS = forumArticleService.selectAllArticlePage(i, 5);
        while(articleDetailVOS.size()!=0){
            List<ArticleElasticsearch> articles = new ArrayList<>();
            for (ArticleDetailVO articleDetailVO:articleDetailVOS){
                ArticleElasticsearch articleElasticsearch = new ArticleElasticsearch();
                BeanUtils.copyProperties(articleDetailVO,articleElasticsearch);
                articles.add(articleElasticsearch);
            }
            repository.saveAll(articles);
            i++;
            log.info("成功新增第{}页数据",i);
            articleDetailVOS = forumArticleService.selectAllArticlePage(i, 5);
        }
        log.info("向ES中存入数据完毕!!!!!!!!!!!!!");
    }

    @Override
    public List<ArticleDetailVO> search(String keyword) {
        /**
         * 获取到ES中符合的数据
         */
        Iterable<ArticleElasticsearch> articles = repository.querySearch(keyword);
        List<ArticleDetailVO> list = new ArrayList<>();
        for (ArticleElasticsearch a:articles
             ) {
            Long id = a.getId();
            ArticleDetailVO articleDetailVO = forumArticleService.selectArticleById(id);
            list.add(articleDetailVO);
        }
        return list;
    }

    @Override
    public JsonPage<ArticleDetailVO> search(String keyword, Integer page, Integer pageSize) {
        Page<ArticleElasticsearch> articles = repository.querySearch(keyword, PageRequest.of(page - 1, pageSize));
        JsonPage<ArticleDetailVO> jsonPage = new JsonPage<>();
        //当前页码
        jsonPage.setPageNum(page);
        //每页条数
        jsonPage.setPageSize(pageSize);
        //赋值总页数
        jsonPage.setTotalPages(articles.getTotalPages());
        //赋值总条数
        jsonPage.setTotalCount(articles.getTotalElements());
        //赋值分页数据
        List<ArticleDetailVO> list = new ArrayList<>();
        for (ArticleElasticsearch a:articles
             ) {
            Long id = a.getId();
            ArticleDetailVO articleDetailVO = forumArticleService.selectArticleById(id);
            list.add(articleDetailVO);
        }
        jsonPage.setList(list);
        return jsonPage;
    }
}
